package md.mru.core.repositories;

import md.mru.core.domains.administration.TreeNodeModel;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * @author sergiu.marin
 * @since 17.04.2014.
 */
public interface TreeNodeModelRepository extends JpaRepository<TreeNodeModel, Long> {

    @Query("SELECT t FROM TreeNodeModel t LEFT JOIN FETCH t.treeFolders f LEFT JOIN FETCH t.groups")
    public List<TreeNodeModel> loadTreeNodeModels();

    @Query("SELECT t FROM TreeNodeModel t LEFT JOIN FETCH t.treeFolders f LEFT JOIN FETCH t.groups g WHERE g.id.userGroup.id=:userGroupId ORDER BY t.position")
    public List<TreeNodeModel> loadTreeNodeModels(@Param(value = "userGroupId") long userGroupId);

    @Modifying
    @Query("UPDATE TreeNodeModel t SET t.position=:position WHERE t.id=:treeId")
    public void savePosition(@Param(value = "position") Double position, @Param(value = "treeId") Long treeId);

    @Modifying
    @Query("UPDATE TreeNodeModel t SET t.name=:name, t.description=:description WHERE t.id=:nodeId")
    void update(@Param(value = "nodeId") long nodeId, @Param(value = "name") String name, @Param(value = "description") String description);
}
